using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class InGameUI : UIBase
{
    private PlayerData playerData;
    private SkillManager skill;
    [SerializeField] private Image blackHoleMask;
    [SerializeField] private Image dashMask;
    [SerializeField] private Image crystalMask;
    [SerializeField] private Image swordMask;
    [SerializeField] private Image counterAttackMask;
    [SerializeField] private Image healthBar;
    [SerializeField] private TextMeshProUGUI currency;


    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();
        playerData = PlayerManager.Instance.Player.GetComponent<PlayerData>();
        if (playerData != null)
        {
            playerData.onHealthChanged += UpdateHealthUI;
        }
        skill = SkillManager.Instance;
        skill.blackHole.onSkillCast += SetSkillMask;
        skill.crystal.onSkillCast += SetSkillMask;
        skill.dash.onSkillCast += SetSkillMask;
        PlayerManager.Instance.OnMoneyChanged += UpdateCurrency;
        UpdateCurrency();        

    }

    private void UpdateHealthUI()
    {
        healthBar.fillAmount = (float)playerData.currentHealth / (float)playerData.GetMaxHealthValue();
    }

    private void UpdateCurrency()
    {
        currency.text = PlayerManager.Instance.Currency.ToString("N0"); 
    }

    // Update is called once per frame
    void Update()
    {
        //if (Input.GetKeyDown(KeyCode.Mouse1) && HasNoSword())
        //    stateMachine.ChangeState(player.aimSword);

        //if (Input.GetKeyDown(KeyCode.Q))
        //{
        //    SetSkillCoolDown(counterAttackMask,skill.);
        //}

        CheckSkillCoolDown(blackHoleMask, skill.blackHole.cooldown);
        CheckSkillCoolDown(crystalMask, skill.crystal.cooldown);
        CheckSkillCoolDown(dashMask, skill.dash.cooldown);
    }

    private void SetSkillMask(Skill _skill)
    {
        Image _image = null;
        if(_skill is BlackHoleSkill)
        {
            _image = blackHoleMask;
        }
        else if(_skill is CrystalSkill)
        {
            _image = crystalMask;
        }
        else if (_skill is DashSkill)
        {
            _image = dashMask;
        }
        if (_image != null && _image.fillAmount <= 0)
        {
            _image.fillAmount = 1;
        }
    }

    void CheckSkillCoolDown(Image _image, float _coolDown)
    {
        if (_image.fillAmount > 0)
            _image.fillAmount -= 1 / _coolDown * Time.deltaTime;
    }




}
